const app = getApp()

const behavior = require('../relation_behavior')
import { HiddenCall } from '../../request/api.js'
import { throttle } from '../../utils/throttle.js'
const request = new HiddenCall
Component({
  /**
   * 视频上传
   * - @author           王磊
   * videoModel        视频上传model
   * isVideo                打开上传窗口
   * code                    房客编号
   */
  behaviors: [behavior],
  properties: {
    videoModel: Array,
    isVideo: Boolean,
    code: String
  },
  methods: {
    /**
     * 上传视频
     * author dzl
     */
    addVideo() {
      // 调用父事件
      this.triggerEvent('addVideo')
    },
    /**
     * 删除要上传视频
     * author  wl
     */
    deleteVideo(e) {
      // 获取删除视频的索引
      let index = e.currentTarget.dataset.videoindex
      // 进行删除
      this.data.videoModel.splice(index, 1)
      // 数据更新
      this.setData({
        videoModel: this.data.videoModel
      })
    },
    /**
     * 上传视频
     */
    addDetailVideo: throttle(function() {
      // 视频数组不为空
      if (this.data.videoModel.length > 0) {
        // 汇集上传数据
        let model = {
          "resourceCode": this.data.code,
          "urls": this.data.videoModel,
          "utype": "1003"
        }
        // 调用视频上传接口
        request.addMedia(model).then(res => {
          // 关闭视频上传窗口，清空视频上传数组
          this.setData({
            isVideo: false,
            videoModel: []
          }) 
          // 上传成功调用父事件清空视频
          this.triggerEvent('clearVideo')
          // 关闭父弹窗
          this.clickMask()
        })
      } else {
        wx.showToast({ title: '请选择视频！', icon: 'none' })
      }
    }, 2000),
    /**
     * 关闭父弹窗
     */
    clickMask() {
      this.triggerEvent('clickMask')
    }
  }
})